// ActionScript file
package {
    import flash.data.EncryptedLocalStore;
    import flash.display.NativeWindow;
    import flash.display.NativeWindowInitOptions;
    import flash.display.NativeWindowSystemChrome;
    import flash.display.NativeWindowType;
    import flash.display.StageAlign;
    import flash.display.StageScaleMode;
    import flash.events.Event;
    import flash.html.HTMLLoader;
    import flash.net.URLRequest;
    import flash.utils.ByteArray;
    
    public class SettingsWindow extends NativeWindow {
        static private var _window:SettingsWindow = null;
        static private var _settings:Object = null;

        static public function getSettings():Object {
            if (_settings == null) {
                _settings = new Object();
                var ba:ByteArray = EncryptedLocalStore.getItem("email");
                _settings['email'] = (ba != null) ? ba.readUTFBytes(ba.length) : "";
                ba = EncryptedLocalStore.getItem("pwd");
                _settings['pwd'] = (ba != null) ? ba.readUTFBytes(ba.length) : "";
                ba = EncryptedLocalStore.getItem("startup");
                _settings['startup'] = (ba != null) ? (ba.readUTFBytes(ba.length) != "0") : true;
            }
            return _settings;
                
        }
        
        static private function saveSettings():void {
            var ba:ByteArray;
            if (_settings['email'] != undefined) {
                ba = new ByteArray();
                ba.writeUTFBytes(_settings['email']);
                EncryptedLocalStore.setItem('email', ba);
            }     
            if (_settings['pwd'] != undefined) {
                ba = new ByteArray();
                ba.writeUTFBytes(_settings['pwd']);
                EncryptedLocalStore.setItem('pwd', ba);
            }     
            if (_settings['startup'] != undefined) {
                ba = new ByteArray();
                ba.writeUTFBytes(_settings['startup'] ? "1" : "0");
                EncryptedLocalStore.setItem('startup', ba);
            }     
        }
        
        static public function show():void {
            if (_window != null) { 
                _window.activate();
            } else {
                var html:HTMLLoader = new HTMLLoader();
                html.addEventListener(Event.COMPLETE, function (e:Event):void {
                    if (_window != null || html.contentWidth == 0)
                        return;         
                        
                    html.width = html.contentWidth;
                    html.height = html.contentHeight;
                    html.window.data = getSettings();
                    html.window.loadValues(); 
                    html.window.saveValuesAS = function ():void {
                        saveSettings();
                        _window.close();
                    }; 
                    html.window.cancelAS = function ():void {
                        _window.close();  
                    };
    
                    _window = new SettingsWindow(html);
                    _window.activate();   
                });        
                html.textEncodingOverride = "cp1251";   
                html.load(new URLRequest("settings.html")); 
            }    
        }
        
        public function SettingsWindow(html:HTMLLoader) {
            var ops:NativeWindowInitOptions = new NativeWindowInitOptions();
            ops.systemChrome = NativeWindowSystemChrome.STANDARD;
            ops.type = NativeWindowType.NORMAL;
            ops.transparent = false;   
            ops.resizable = false;  
            ops.maximizable = false;
            ops.minimizable = false;
            super(ops);
            stage.stageWidth = html.contentWidth + 10;
            stage.stageHeight = html.contentHeight + 10;  
            stage.scaleMode = StageScaleMode.NO_SCALE;
            stage.align = StageAlign.TOP_LEFT;
            title = "ВКонтакте-Новости";

            html.x = 5;
            html.y = 5;  
            stage.addChild(html);
            
            this.addEventListener(Event.CLOSE, onClose);  
        }
        
        private function onClose(e:Event):void {
            _window = null;
        }
    }
}